<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'deletelabelpic.jsp' starting page</title>
   
    <meta http-equiv="content-type" content="no-cache, must-revalidate" />
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	 <meta http-equiv="expires" content="0"> 
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
	<script>
				function prochange(){
		     		var proid= $("#productselect").val();
		     		if(proid=="xx"){
		     					document.getElementById("label").length=0;
								var o=document.createElement("option");
								o.innerHTML="--请选择款式--";
						    	o.setAttribute("value","yy");
						    	document.getElementById("label").appendChild(o);
						    	$("#showpicdiv").html("");
		     		}else{
		     					$.getJSON("getLabelByProductIdAction",{"productId":proid},function(data){
								if(data.labels.length>0){
									document.getElementById("label").length=0;
									var o=document.createElement("option");
							    	o.innerHTML="--请选择款式--";
							    	o.setAttribute("value","0000");
							    	document.getElementById("label").appendChild(o);
					    			$.each(data.labels,function(index,value){
						    			var o=document.createElement("option");
								    	o.innerHTML=value.labelName+"";
								    	o.setAttribute("value",value.labelId);
								    	document.getElementById("label").appendChild(o); 
			    					});
								}else{
									document.getElementById("label").length=0;
									var o=document.createElement("option");
									o.innerHTML="--未上传款式--";
							    	o.setAttribute("value","0000");
							    	document.getElementById("label").appendChild(o);
								}
					});	     
		     		}
	     	}
	     	function deletepic(picurl,flag){
		     	if(window.confirm("确定要删除么")){
		     	//alert(value.picUrl);
		     				$.post("deleteLabelPicByPicUrlAction",{"picUrl":picurl},function(data){
		     				if(data="success"){
		     					alert("删除成功");
		     					if(flag==1){
		     						prochange();
		     					}else if(flag==2){
		     						labelchange();
		     					}
		     				}else{
		     					alert("删除失败");
		     				}
		     		});
		     	}else{}
	     	}
				function labelchange(){
				    var labelId = $("#label").val();
				    var html="<form action=updateLabelCostByMap method=post onsubmit='return checkInput()'>"
				     +"<input type=hidden name=labelId value="+labelId+">";
					var labelId=$("#label").val();
					if(labelId=="0000"){
						prochange();
					}else{
						$.getJSON("showLabelCost",{"labelId":labelId},function(data){
						html = html+"<div id=labeldiv><img src="+data.label.bgPic+" width=200px>"+data.label.labelName+"<br>";
						   $.each(data.labelCosts,function(i,v){
						   if(v.price!=null){
						   html = html+"<div id="+v.labelcostId+">"+v.labelcostName+": <input type=text name=labelprice["+v.labelcostId+"] value="+v.price+" /><br></div>";
						   }
						   });
						   html = html+"</div><input type=submit value=修改  /></from>";
						   $.each(data.labelCosts,function(i,v){
						   if(v.price!=null){
						   html = html
						   +" <input type=checkbox value="+v.labelcostId+" name=labelcostId checked onclick=changeLabel('"+v.labelcostName+"',"+v.price+","+v.labelcostId+") />"+v.labelcostName;
						   }else{
						  html = html
						   +" <input type=checkbox value="+v.labelcostId+" name=labelcostId onclick=changeLabel('"+v.labelcostName+"',"+v.price+","+v.labelcostId+") />"+v.labelcostName;
						   }
						   });
						   html = html;
						   $("#showpicdiv").html(html);
						});	
					}
					//alert(labelId);
				}
				
				function checkInput(){
				var text = $(":text");
				if(text.length<1){
				  alert("请选择成本标签");
				  return false;
				}
				  for(var i=0;i<text.length;i++){
				   var t = $.trim($(":text:eq("+i+")").val());
					   if(isNaN(t)||t==''){
					     alert("数据不正确");
					     return false;
					   }
					   $(":text:eq("+i+")").val(t)
				  }
				  return true;
				}
				
				function changeLabel(name,price,id){
				if(price==null){
				  price="";
				}
				   var labeldiv = $("#labeldiv");
				  if($("#"+id).html()==null){
				   var html = "<div id="+id+">"+name+": <input type=text name=labelprice["+id+"] value="+price+"><br></div>";
				     $("#labeldiv").append(html);
				   }else{
				      $("#"+id).remove();
				   }
				}
	</script>
  </head>
  
  <body>
  <form action="" onsubmit='return checkInput()'></form>
  		<div align="center"><h2>配置成本</h2></div>
   		<div  align="center">
			<select name="product"  id="productselect" style="width: 135px;" onchange="prochange()">
				<option value="xx">--请选择产品--</option>
				<c:forEach items="${product}" var="pro">
					<option value="${pro.productId}">${pro.productName}</option>
				</c:forEach>
			</select>
			<select name="label"  id="label" style="width: 135px;" onchange="labelchange()">
				<option value="yy">--请选择款式--</option>
			</select>
		</div>
		<div align="center" id="showpicdiv"></div>
  </body>
</html>
